let MS = require("./ms.js")
const request = require("./request.js")
const Error = require("./error");
const storage = require("./storage.js")
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isArray,
    isBoolean
} = require('./dataType')

const common = class common {
    constructor() {
        //this.currUser = MS.User.current()
    }

    checkExpressFee(totalPrice, totalShareProfit, cityName, ice, isAgent) {
        let data = {
            totalPrice: totalPrice,
            totalShareProfit: totalShareProfit,
            cityName: cityName,
            ice: ice,
            isAgent: isAgent
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.COMMON.CHECK_EXPRESS_FEE.url,
                MS._config.parameters.COMMON.CHECK_EXPRESS_FEE.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadHotSearch() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.COMMON.LOAD_HOT_SEARCH.url,
                MS._config.parameters.COMMON.LOAD_HOT_SEARCH.method, {}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    sendSMSCode(options) {
        let data = {
            template: '短息验证'
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.phone) && isString(options.phone)) {
                data.phone = options.phone
            }
            if(!isUndefined(options.template) && isString(options.template)) {
                data.template = options.template
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.COMMON.SEND_SMS_CODE.url,
                MS._config.parameters.COMMON.SEND_SMS_CODE.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    verifySmsCode(phoneNumber, code) {
        let data = {
            userId: MS.User.current().id,
            phone: phoneNumber,
            code: code
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.COMMON.VERIFY_SMS_CODE.url,
                MS._config.parameters.COMMON.VERIFY_SMS_CODE.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    createQrCodeUnlimit(userId, scene, path) {
        let params = {
            userId: userId,
            scene: scene,
            path: path
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.APP.CREATE_QR_CODE_UNLIMIT.url,
                MS._config.parameters.APP.CREATE_QR_CODE_UNLIMIT.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    createGoodsQrCodeUnlimit(options) {
        let params = {
            path: 'pages/goodsDetail/goodsDetail'
        }
        if (!isUndefined(options.goodsId) && isString(options.goodsId)) {
            params.goodsId = options.goodsId;
        }
        if (!isUndefined(options.scene) && isString(options.scene)) {
            params.scene = options.scene;
        }
        if (!isUndefined(options.path) && isString(options.path)) {
            params.path = options.path;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.APP.CREATE_GOODS_QR_CODE_UNLIMIT.url,
                MS._config.parameters.APP.CREATE_GOODS_QR_CODE_UNLIMIT.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findAreaTree() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.AREA.FIND_TREE.url,MS._config.parameters.AREA.FIND_TREE.method, {}).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            })
        })
    }

    findProvince() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.AREA.FIND_PROVINCE.url, MS._config.parameters.AREA.FIND_PROVINCE.method, {}).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            })
        })
    }

    findCity(provinceCode) {
        let params = {
            provinceCode: provinceCode
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.AREA.FIND_CITY.url,MS._config.parameters.AREA.FIND_CITY.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            })
        })
    }

    findCounty(cityCode) {
        let params = {
            provinceCode: cityCode
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.AREA.FIND_COUNTY.url,MS._config.parameters.AREA.FIND_COUNTY.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            })
        })
    }

    loadCity(lng, lat) {
        var url = 'https://api.map.baidu.com/geocoder/v2/?ak=H1ZiOWFkOufMfMKL0wEtlkAp&location=' + lat + ',' + lng + '&output=json'
        return new Promise((resolve, reject) => {
            request(url, "GET", {}).then(res => {
                // console.info(res);
                // if(res.status == 0) {
                //     resolve(res.result)
                // } else {
                //     reject(res.errMsg)
                // }
                resolve(res);
            })
        })
    }
}

module.exports = common